package com.atguigu.dataSource;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.apache.commons.dbcp.BasicDataSourceFactory;
import org.junit.Test;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @author hjwStrart
 * @time 2022-03-09 22:46
 * @description com.atguigu.dataSource
 * @version: 1.0
 */
public class DruidDemo {

//    和DBCP几乎一模一样
    @Test
    public void test() throws SQLException {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql:///test?serverTimezone=UTC");
        dataSource.setUsername("root");
        dataSource.setPassword("hjw123..");
        Connection conn = dataSource.getConnection();
        System.out.println(conn);

    }

//    和DBCP几乎一模一样
    @Test
    public void test2() throws Exception {
        Properties properties = new Properties();
        InputStream is = ClassLoader.getSystemResourceAsStream("druid.properties");
        properties.load(is);
        DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
        System.out.println(dataSource);
        Connection conn = dataSource.getConnection();
        System.out.println(conn);
    }
}
